var myChart3 = echarts.init(document.getElementById("zhichu_chart"));
var myChart4 = echarts.init(document.getElementById("jihuan_chart"));

$(".content").resize(function () {
  myChart3.resize();
  myChart4.resize();
});
$("div").resize(function () {
  myChart3.resize();
  myChart4.resize();
});

$(function () {
  yewuData = 1;
  inityewuData();

  document.getElementById("yewuDisplay").style.display = "none";
  document.getElementById("zijinDisplay").style.display = "none";
  document.getElementById("caiwuDisplay").style.display = "none";


})


function inityewuData() {
  const currYear = localStorage.getItem("year");
  const currMon = localStorage.getItem("month");
  const currDept = localStorage.getItem("dept");

  var obj = {
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 501,
  };
  ApiGet("getdata", obj, function (e) {
    // console.log(e)
    var json = JSON.parse(e);
    console.log(json)

    var chartData = json.items[0].charts;
    console.log(chartData, "chartData");

    // getyewuChart(chartData[0].data) //业务
    getyewuNum(chartData[1].data); //业务数字
    getzijinChart(chartData[2].data); //资金
    getzijinNum(chartData[3].data); //资金数字
    // getcaiwuChart(chartData[4].data) //财务
    getcaiwuNum(chartData[5].data); //财务数字
    monthlyTrend(chartData[6].data); //月度趋势

    //权限
    let permi = JSON.parse(localStorage.getItem('permission'));
    console.log(permi, 'permi')
    if (permi) {
      if (permi.key01 == '1') {
        document.getElementById("yewuDisplay").style.display = "block";
      }
      if (permi.key02 == '1') {
        document.getElementById("zijinDisplay").style.display = "block";
      }
      if (permi.key03 == '1') {
        document.getElementById("caiwuDisplay").style.display = "block";
      }
    } else {
      window.location = 'login.html'
    }

  });
}

function getyewuNum(data) {
  let yewuNum = data;
  // 计划完成数
  $(".planComplete").text(yewuNum[0].key01);
  // 计划总数
  $(".planTotal").text(yewuNum[0].key02);
  // 预算执行
  $(".budgetImplementation").text(
    pricefmt((yewuNum[0].key04 / 10000).toFixed(0))
  );
  // 支出总预算
  $(".totalexpenditureBudget").text(
    pricefmt((yewuNum[0].key05 / 10000).toFixed(0))
  );
  //本月支出

  //预算执行金额chart
  let zhichu = (yewuNum[0].key03 * 100).toFixed(2);
  var zhichu_chart = {
    series: [{
      type: 'liquidFill',
      radius: '95%',

      //水波
      color: [
        "#E8EFFF",
        "#BBCFFF",
        new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            offset: 0.8,
            color: "#165DFF",
          },
          {
            offset: 1,
            color: "#165DFF",
          },
        ]),
      ],
      amplitude: 5, //水波振幅
      data: [yewuNum[0].key03, yewuNum[0].key03, yewuNum[0].key03],
      center: ["50%", "50%"],
      label: {
        normal: {
          formatter: zhichu + "%",
          fontSize: 16,
          fontWeight: 400,
          color: "#72D1FB",
        },
      },
      itemStyle: {
        //opacity: 0.7, // 波浪的透明度
        shadowBlur: 0, // 波浪的阴影范围
      },
      emphasis: {
        itemStyle: {
          opacity: 0.8, // 鼠标经过波浪颜色的透明度
        },
      },
      outline: {
        borderDistance: 0,
        itemStyle: {
          borderWidth: 2.5,
          borderColor: '#165DFF',
          shadowBlur: 20
        }

      },
      backgroundStyle: {
        color: "#fff",
      },
    }, ],
  };
  myChart3.setOption(zhichu_chart);

  // 计划完成数chart
  let jihuaNum = (yewuNum[0].key06 * 100).toFixed(2);
  var jihuan_chart = {
    series: [{
      type: 'liquidFill',
      radius: '95%',
      //水波
      color: [
        "#E8EFFF",
        "#BBCFFF",
        new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
            offset: 0.8,
            color: "#165DFF",
          },
          {
            offset: 1,
            color: "#165DFF",
          },
        ]),
      ],
      data: [yewuNum[0].key06, yewuNum[0].key06, yewuNum[0].key06],
      center: ["50%", "50%"],
      amplitude: 5, //水波振幅
      label: {
        normal: {
          formatter: jihuaNum + "%",
          fontSize: 16,
          fontWeight: 400,
          color: "#72D1FB",
        },
      },
      itemStyle: {
        //opacity: 0.7, // 波浪的透明度
        shadowBlur: 0, // 波浪的阴影范围
      },
      emphasis: {
        itemStyle: {
          opacity: 0.8, // 鼠标经过波浪颜色的透明度
        },
      },
      outline: {
        borderDistance: 0,
        itemStyle: {
          borderWidth: 2.5,
          borderColor: '#165DFF',
          shadowBlur: 20
        }
      },
      backgroundStyle: {
        color: "#fff",
      },
    }, ],
  };
  myChart4.setOption(jihuan_chart);
}

function getzijinChart(data) {
  //资金表
  let zijinliuru = [];
  let zijinliuchu = [];
  let xianjindengjia = [];

  data.forEach((item) => {
    //期末现金及等价物余额
    $(".xianjindengjia").text(pricefmt((item.key04 / 10000).toFixed(0)));
    zijinliuru.push((item.key02 / 10000).toFixed(2));
    zijinliuchu.push((item.key03 / 10000).toFixed(2));
    xianjindengjia.push((item.key04 / 10000).toFixed(2));
  });
}

function getzijinNum(data) {
  let zijinNum = data;
  // 余额
  // $(".balance").text(FormatCash(zijinNum[0].key01));
  // 期初
  $(".beginning").text(pricefmt((zijinNum[0].key02 / 10000).toFixed(0)));
  // 流入
  $(".inflow").text(pricefmt((zijinNum[0].key03 / 10000).toFixed(0)));
  // 流出
  $(".flowout").text(pricefmt((zijinNum[0].key04 / 10000).toFixed(0)));
}

function getcaiwuNum(data) {
  let caiwuNum = data;
  //财务数据
  // 本月收入
  $(".monthIncome").text(pricefmt((caiwuNum[0].key01 / 10000).toFixed(0)));
  // 本年收入
  $(".yearIncome").text(pricefmt((caiwuNum[0].key02 / 10000).toFixed(0)));
  // 本月利润
  $(".monthProfit").text(pricefmt((caiwuNum[0].key03 / 10000).toFixed(0)));
  // 本年利润
  $(".yearProfit").text(pricefmt((caiwuNum[0].key04 / 10000).toFixed(0)));
}

function monthlyTrend(data) {
  console.log(data, "月度趋势");
  let yueduqushiyusuan = []; //支出
  let yueduqushizhixing = []; //执行
  data.forEach((item) => {
    yueduqushiyusuan.push((item.key02 / 10000).toFixed(2));
    yueduqushizhixing.push((item.key03 / 10000).toFixed(2));
  });
  let xData = [
    "1月",
    "2月",
    "3月",
    "4月",
    "5月",
    "6月",
    "7月",
    "8月",
    "9月",
    "10月",
    "11月",
    "12月",
  ];
  let legendData = ["全面预算支出", "执行"];
  let Data = [{
      name: "全面预算支出",
      type: "bar",
      barWidth: "25",
      barGap: 0,
      itemStyle: {
        color: "#165DFF",
      },
      data: yueduqushiyusuan,
    },
    {
      name: "执行",
      type: "bar",
      barWidth: "25",
      barGap: 0,
      itemStyle: {
        color: "#50CD89",
      },
      data: yueduqushizhixing,
    },
  ]
  have('monthlyTrend') ? yueduqushi('monthlyTrend', xData, legendData, Data) : ''
}

function quanxian(data) {
  console.log(data, "权限");
  // 首页业务区域
  let yewuDisplay = data[0].key01
  // 首页资金区域
  let zijinDisplay = data[0].key02
  // 首页财务区域
  let caiwuDisplay = data[0].key03
  if (yewuDisplay == '0') {
    document.getElementById("yewuDisplay").style.display = "none";
  }
  if (zijinDisplay == '0') {
    document.getElementById("zijinDisplay").style.display = "none";
  }
  if (caiwuDisplay == '0') {
    document.getElementById("caiwuDisplay").style.display = "none";
  }
}